<?php
$T->loadTpl("./html/".$template."/".$qname.".html");
$T->SetTpl('cssjs','html/cssjs.inc'); 
$T->SetTpl('jsmodel','html/jsmodel.inc');     
 
$today = date('Y-m-d');
$now = time();
$rest = $T->db->query("select am_on,am_off,pm_on,pm_off from sys_rest where role=".$_SESSION['role'])->fetch(PDO::FETCH_ASSOC);
$attend = $T->db->query("select attend_am_on,attend_am_off,attend_pm_on,attend_pm_off,late_am,early_am,late_pm,early_pm from sys_attend where uid='".$_SESSION['uid']."' and date='".$today."'")->fetch(PDO::FETCH_ASSOC);
if($attend){
    if($attend['attend_am_on'])$T->Set("attend_am_on",date('H:i:s',$attend['attend_am_on']));
    if($attend['attend_am_off'])$T->Set("attend_am_off",date('H:i:s',$attend['attend_am_off']));
    if($attend['attend_pm_on'])$T->Set("attend_pm_on",date('H:i:s',$attend['attend_pm_on']));
    if($attend['attend_pm_off'])$T->Set("attend_pm_off",date('H:i:s',$attend['attend_pm_off']));
    if(isset($attend['late_am']))$T->Set("late_am",$attend['late_am']);
    if(isset($attend['early_am']))$T->Set("early_am",$attend['early_am']);
    if(isset($attend['late_pm']))$T->Set("late_pm",$attend['late_pm']);
    if(isset($attend['early_pm']))$T->Set("early_pm",$attend['early_pm']);
}  
$sys_attend_time = $T->db->query("select * from sys_attend_time limit 1")->fetch(PDO::FETCH_ASSOC);
$am_on_time_before = strtotime($rest['am_on']) - $sys_attend_time['on_before']*60;  
$am_on_time_after = strtotime($rest['am_on']) + $sys_attend_time['on_after']*60+60; 
$am_off_time_before = strtotime($rest['am_off']) - $sys_attend_time['off_before']*60;  
$am_off_time_after = strtotime($rest['am_off']) + $sys_attend_time['off_after']*60+60;
$pm_on_time_before = strtotime($rest['pm_on']) - $sys_attend_time['on_before']*60;  
$pm_on_time_after = strtotime($rest['pm_on']) + $sys_attend_time['on_after']*60+60; 
$pm_off_time_before = strtotime($rest['pm_off']) - $sys_attend_time['off_before']*60;  
$pm_off_time_after = strtotime($rest['pm_off']) + $sys_attend_time['off_after']*60+60;
if($now<=$am_on_time_after&&$now>=$am_on_time_before){
    $attendis = 1;
}elseif($now<=$am_off_time_after&&$now>=$am_off_time_before){
    $attendis = 2;
}elseif($now<=$pm_on_time_after&&$now>=$pm_on_time_before){
    $attendis = 3;
}elseif($now<=$pm_off_time_after&&$now>=$pm_off_time_before){
    $attendis = 4;
}else{
    $attendis = 0;
}
$T->Set("today",$today);
$T->Set("attendis",$attendis);
$T->Set("am_on",$rest['am_on']);
$T->Set("am_off",$rest['am_off']);
$T->Set("pm_on",$rest['pm_on']);
$T->Set("pm_off",$rest['pm_off']); 

$T->Set("template",$template);         
$T->clearNaN();       
$T->clearNoN();
$T->display(); 